package com.nekoneko.nekonekodemo.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

/**
 * Created by 想法的猫 on 2018/1/10 0010.
 */

public class TouchRelative extends FrameLayout {
    public TouchRelative(Context context) {
        super(context);
    }

    public TouchRelative(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TouchRelative(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
        Log.d("TouchRelative", "measureChild");
        logMode(parentWidthMeasureSpec, parentHeightMeasureSpec);
        super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
    }


    @Override
    protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d("TouchRelative", "measureChildren");
        super.measureChildren(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void addView(View child) {
        Log.d("TouchRelative", "addView=" + child.getLayoutParams());
        super.addView(child);
    }

    @Override
    protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
        Log.d("TouchRelative", "measureChildWithMargins" + child.getLayoutParams().height);
        logMode(parentWidthMeasureSpec, parentHeightMeasureSpec);
        super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        logMode(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void logMode(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        Log.d("TouchRelative", "widthMode=" + getModeName(widthMode) + " widthSize=" + widthSize + " heightMode=" + getModeName(heightMode) + " heightSize=" + heightSize);
    }

    private String getModeName(int mode) {
        switch (mode) {
            case MeasureSpec.AT_MOST:
                return "AT_MOST";
            case MeasureSpec.EXACTLY:
                return "EXACTLY";
            case MeasureSpec.UNSPECIFIED:
                return "UNSPECIFIED";
        }
        return "";
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d("TouchRelative", "dispatchTouchEvent");
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d("TouchRelative", "onInterceptTouchEvent");
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("TouchRelative", "onTouchEvent");
        return super.onTouchEvent(event);
    }
}
